package com.atzongmaotan.reggie.controller;

import com.atzongmaotan.reggie.common.R;
import com.atzongmaotan.reggie.dto.SetmealDto;
import com.atzongmaotan.reggie.entity.Category;
import com.atzongmaotan.reggie.entity.Setmeal;
import com.atzongmaotan.reggie.service.CategoryService;
import com.atzongmaotan.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:    SetmealController
 * Package:  com.atzongmaotan.controller
 * Description:
 *
 * @Author: interface
 * @Create: 2024/6/14 - 上午9:01
 * @Version: v1.0
 */
@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info("套餐为:{} ",setmealDto.toString());
        setmealService.saveSetmealAndSetmealDish(setmealDto);
        return R.success("添加套餐成功");
    }
    @GetMapping("page")
    public R<Page<SetmealDto>> pageSetmeal(@RequestParam int page, @RequestParam int pageSize,String name) {
        log.info("page:{} , pageSize:{} , name:{}",page,pageSize,name);
        Page<Setmeal> pageSetmeal = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageSetmeal,wrapper);

        List<Setmeal> records = pageSetmeal.getRecords();
        List<SetmealDto> setmealDtoList = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
               setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        Page<SetmealDto> pageSetmealDto = new Page<>(page,pageSize);
        BeanUtils.copyProperties(pageSetmeal,pageSetmealDto,"records");
        pageSetmealDto.setRecords(setmealDtoList);

        return R.success(pageSetmealDto);
    }
    @DeleteMapping
    public R<String> deleteSetmeal(@RequestParam("ids") List<Long> idList) {
        log.info("idList:{}",idList);
        setmealService.removeWithDish(idList);
        return R.success("删除套餐成功");
    }

    /**
     * 根据条件获取套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        log.info("套餐为:{} ",setmeal.toString());
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }
}
